import {
  exportUrl,
  getMaterialCodeList,
  getBomStatusList,
  getMaterialTypeValueList,
  getManufactureMethodValueList,
  getBomVersionList,
} from "@/api/materials/mesProdBom";

import { getLineCodeList } from "@/api/employee/mesTeam";
import { exportExcel } from "@/utils/judge";
import { downloadTemplateUrl } from "@/api/materials/mesProdBom";
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", //点击时返回的标识
    url: exportUrl,
  },
  {
    name: $i18n.t("common.add"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "add", //点击时返回的标识
  },
  {
    name: $i18n.t("common.edit"),
    type: "warning",
    icon: "el-icon-edit-outline",
    isMultiple: true, //至少选择一条
    //是否可点击；
    disabled: (data) => {
      let bomStatus = data[0]?.bomStatus ?? "";
      let prid = data[0]?.parentId ?? "";
      return prid != "0" || data.length != 1 || bomStatus == 1;
    },
    handleName: "edit", //点击时返回的标识
  },
  {
    name: $i18n.t("common.import"),
    type: "primary",
    icon: "el-icon-upload2",
    handleName: "import", //点击时返回的标识
  },
  {
    name: $i18n.t("mesProdBom.publish"),
    type: "success",
    icon: "el-icon-s-promotion",
    isMultiple: true, //至少选择一条
    //是否可点击；
    disabled: (data) => {
      let bomStatus = data[0]?.bomStatus ?? "";
      let prid = data[0]?.parentId ?? "";
      return prid != "0" || data.length != 1 || bomStatus == 1;
    },
    handleName: "publish", //点击时返回的标识
  },
  {
    name: $i18n.t("mesProdBom.deactivate"),
    type: "warning",
    icon: "el-icon-error",
    handleName: "deactivate", //点击时返回的标识

    isMultiple: true, //至少选择一条
    //是否可点击；
    disabled: (data) => {
      console.log(data);
      let bomStatus = data[0]?.bomStatus ?? "";
      let prid = data[0]?.parentId ?? "";
      return prid != "0" || data.length != 1 || bomStatus == 2;
    },
  },
  {
    name: $i18n.t("common.copy"),
    type: "primary",
    icon: "el-icon-s-open",
    handleName: "copy", //点击时返回的标识

    isMultiple: true, //至少选择一条
    //是否可点击；
    disabled: (data) => {
      let prid = data[0]?.parentId ?? "";
      return prid != "0" || data.length != 1;
    },
  },
  {
    name: $i18n.t("mesProdBom.upgrade"),
    type: "primary",
    icon: "el-icon-top",
    isMultiple: true, //至少选择一条
    //是否可点击；
    disabled: (data) => {
      let prid = data[0]?.parentId ?? "";
      return prid != "0" || data.length != 1;
    },
    handleName: "upgrade", //点击时返回的标识
  },

  // {
  //   name: "删除",
  //   type: "danger",
  //   icon: "el-icon-delete",
  //   handleName: "deleteBatch", //点击时返回的标识
  //   disabled: (data) => {
  //     console.log(data);
  //     return !data.length > 0;
  //   },
  // },
];
// 行末按钮配置
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit", //点击时返回的标识
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "detail", //点击时返回的标识
  },
  // {
  //   name: "删除",
  //   type: "danger",
  //   class: "row-btn-danger",
  //   handleName: "delete", //点击时返回的标识
  // },
];
// 搜索条件配置
export const searchConfig = {
  inline: true,
  formDesc: {
    materialCode: {
      type: "input",
      label: $i18n.t("mesProdBom.materialCode"),
    },
    materialName: {
      type: "input",
      label: $i18n.t("mesProdBom.materialName"),
    },
    bomStatus: {
      type: "select",
      label: $i18n.t("common.status"),
      prop: {
        value: "value",
        text: "description",
      },

      options: async (data) => {
        const list = await getBomStatusList();
        return list.data.result;
      },
    },
    bomVersion: {
      type: "input",
      label: $i18n.t("mesProdBom.version"),
    },
  },
};
// 新增/删除表单配置
export const formDescDialog = {
  data () {
    const _this = this;
    return {
      banbenList: [],
      bomRules: {
        quantity: {
          required: true,
          message: $i18n.t("mesProdBom.inputQuantity"),
        },
      },
      //bomTableData: [],
      bomColumns: [
        {
          // el-table-column 的属性
          type: "index",
          width: 50,
        },
        {
          // el-table-column 的属性
          prop: "materialCode",
          label: $i18n.t("mesProdBom.materialCode"),
          minWidth: 120,
          showOverflowTooltip: true,
        },
        {
          prop: "materialName",
          label: $i18n.t("mesProdBom.materialName"),
          minWidth: 120,
          showOverflowTooltip: true,
        },
        {
          prop: "manufactureMethodName",
          label: $i18n.t("mesProdBom.manufactureMethod"),
          minWidth: 160,
        },
        {
          prop: "materialTypeName",
          label: $i18n.t("mesProdBom.materialType"),
          minWidth: 120,
        },
        {
          prop: "bomVersion",
          label: $i18n.t("mesProdBom.bomVersion"),
          minWidth: 100,
        },
        {
          prop: "unit",
          label: $i18n.t("common.unit"),
          // width: 60,
          showOverflowTooltip: true,
        },
        {
          prop: "quantity",
          minWidth: 140,
          label: $i18n.t("mesProdBom.usedQuantity"),
          // 当有 content 属性时, 可以指定相应的组件
          content: {
            // type 可以为全局注册的组件名或者组件引用
            type: "el-input-number",
            // attrs 是组件的属性
            attrs: {
              placeholder: $i18n.t("mesProdBom.inputQuantity"),
              min: 0.0001,
              controlsPosition: "right",
            },
          },
        },
        {
          prop: "advancingStockDays",
          label: $i18n.t("mesProdBom.advancingStockDays"),
          width: 160,
          content: {
            // type 可以为全局注册的组件名或者组件引用
            type: "el-input-number",
            // attrs 是组件的属性
            attrs: {
              placeholder: $i18n.t("mesProdBom.inputQuantity"),
              min: 0,
              stepStrictly: true,
              step: 1,
              controlsPosition: "right",
            },
          },
        },
      ],

      formDescDialog: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        formDesc: {
          materialCode: {
            type: "select",
            label: $i18n.t("mesProdBom.productCode"),
            layout: 8,
            required: true,
            disabled: (d) => !!d.id,
            // prop: {
            //   value: "value",
            //   text: "description",
            // },
            // options: async (data) => {
            //   const list = await getMaterialCodeList();
            //   return list.data.result;
            // },
          },
          materialName: {
            type: "input",
            label: $i18n.t("mesProdBom.productName"),
            disabled: (d) => !!d.id,
            layout: 8,
            required: true,
          },
          bomVersion: {
            type: "input",
            label: $i18n.t("mesProdBom.version"),
            disabled: (d) => !!d.id,
            // disabled: true,
            layout: 8,
            required: false,
          },
          level: {
            type: "input",
            label: $i18n.t("mesProdBom.level"),
            disabled: (d) => !!d.id,
            // disabled: true,
            layout: 8,
            required: false,
          },
          materialTypeValue: {
            type: "input",
            label: $i18n.t("mesProdBom.materialTypeCode"),
            // disabled: true,
            disabled: (d) => !!d.materialTypeValue,
            layout: 8,
            required: true,
          },
          materialTypeName: {
            type: "input",
            label: $i18n.t("mesProdBom.materialTypeName"),
            // disabled: true,
            disabled: (d) => !!d.materialTypeName,
            layout: 8,
            required: true,
          },
          manufactureMethodName: {
            type: "input",
            label: $i18n.t("mesProdBom.manufactureMethodName"),
            disabled: (d) => !!d.id,
            layout: 8,
            required: true,
          },
          serielsCode: {
            type: "input",
            label: $i18n.t("mesProdBom.seriesCode"),
            // disabled: true,
            disabled: (d) => !!d.serielsCode,
            layout: 8,
            required: true,
          },
          serielsName: {
            type: "input",
            label: $i18n.t("mesProdBom.seriesName"),
            // disabled: true,
            disabled: (d) => !!d.serielsName,
            layout: 8,
            required: true,
          },

          mesProdBomList: {
            type: "input",
            label: $i18n.t("mesProdBom.mesProdBomList"),
            layout: 24,
            required: true,
          },
        },
      },
    };
  },
  methods: {
    addList () {
      if (this.formDataDialog.materialCode) {
        this.bomDialogVisible = true;
      } else {
        this.$message.warning($i18n.t("mesProdBom.selectProduct"));
      }
    },
    async getBomVersionList (data, type) {
      data.map(async (item, index) => {
        let versionList = await getBomVersionList({
          materialCode: item.goodsCode || item.materialCode,
        });
        this.$set(
          this.formDataDialog.mesProdBomList[index],
          "banbenList",
          versionList.data.result
        );
        if (type == 1 && versionList.data.result.length > 0) {
          if (!this.formDataDialog.mesProdBomList[index].bomVersion) {
            this.$set(
              this.formDataDialog.mesProdBomList[index],
              "bomVersion",
              versionList.data.result?.[0].bomVersion ?? ""
            );
          }
        }
      });
    },
  },
  created () { },
};

//导入数据
export const importFormDescDialog = {
  span: 24,
  disabled: false,
  isShowSubmitBtn: true,
  formDesc: {
    model: {
      type: "button",
      label: "",
      layout: 24,
      required: false,
      title: $i18n.t("common.downloadTemplate"), // "下载模板",,,
      style: {
        color: "#1867c0",
        fontSize: "14px",
      },
      on: {
        click: () => {
          window.open(downloadTemplateUrl)
        },
      },
    },
    file: {
      type: "input",
      label: $i18n.t("common.chooseFile"), // "文件选择",
      layout: 24,
      // required: true,
    },
  },
};
